/**
 * 控制层
 */
import * as userSerive from '../dao/userDao'
import * as uploadSerive from '../dao/uploadFile'
import {successStatus,errorStatus} from '../plugins/status'
import {sendCode,randomCode} from '../plugins/getMessage'
let msgCode = undefined
const userCorrelation = {
  // 登录
  login: function(req,res) {
    let {username,password,code} = req.body
    if (code && code != msgCode) {
      errorStatus.RESULT.msg = "验证码不正确"
      res.status(200).json(errorStatus.RESULT)
      return
    }
    // 1.创建数据库链接
    if (!username || !password) {
      return
    }
    userSerive.findLogin(username,password,req,res)
  },
  // 注册
  register: function(req, res) {
    let {username,password} = req.body
    if (!username || !password) {
      return
    }
    userSerive.findRegistern(username,password,res)
  },
  // 用户列表
  userList: function (req,res) {
    userSerive.findUserList(res)
  },
  // 退出
  exit: function (req, res) {
    req.session.destroy()
  },
  // 发送短信验证码
  sendMsgCode: function (req,res) {
    msgCode = randomCode(6);//生成6位数字随机验证码
    sendCode(req.body.phone,msgCode,function(success){
        if(success){
          successStatus.RESULT.msg = "短信验证码已发送"
          res.status(200).json(successStatus.RESULT);
        }else{
          errorStatus.RESULT.msg = "短信验证码发送失败"
          res.status(400).json(errorStatus.RESULT);
        }
    })
  },
  // 上传图片
  addImages (req,res) {
    uploadSerive.uploadImages(req,res)
  },
  // 删除图片
  removeImages (req, res) {
    uploadSerive.removeImages(req,res)
  }
}

export default userCorrelation